﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.Finance
{
    public partial class ucExpenses : UserControl
    {
        #region UserControl

        public ucExpenses()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            loadData();
            rbtnExpense_CheckedChanged(this.rbtnExpense, e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = Main.AeroColor;
        }

        #endregion

        #region Event

        #region Button

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.pLeft.Visible = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if ((this.cbbType.SelectedValue.ToString() == "0" && this.cbbBank.Enabled) || (this.cbbType.SelectedValue.ToString() != "0" && !this.cbbBank.Enabled))
            {
                MessageBox.Show("内部错误");
                return;
            }
            if (this.cbbClassify.SelectedItem == null)
            {
                MessageBox.Show("请选择分类");
                return;
            }

            string type = this.rbtnIncome.Checked ? "0" : "1";

            DateTime myDate = new DateTime(this.dtpDate.Value.Year, this.dtpDate.Value.Month, this.dtpDate.Value.Day, this.dtpTime.Value.Hour, this.dtpTime.Value.Minute, this.dtpTime.Value.Second);

            if (MoneyClass.Expenses.Add(Main.CurrentGuid, type, this.cbbType.SelectedValue.ToString(), this.cbbType.SelectedValue.ToString() == "0" ? "" : this.cbbBank.SelectedValue.ToString(), this.cbbClassify.SelectedValue.ToString(), myDate.ToString("yyyy-MM-dd HH:mm:ss"), this.nudCount.Value.ToString(), this.tbComment.Text) != 1)
            {
                MessageBox.Show("操作失败");
                return;
            }

            loadData();
            this.pLeft.Visible = false;
        }

        #endregion

        #region RadioButton

        private void rbtnIncome_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbtnIncome.Checked)
            {
                InitIncomeType();
                loadBank();
                loadIncomeClassify();
            }
        }

        private void rbtnExpense_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbtnExpense.Checked)
            {
                InitExpenseType();
                loadBank();
                loadExpenseClassify();
            }
        }

        #endregion

        #region ComboBox

        private void cbbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbbType.SelectedIndex == 0)  //现金
            {
                this.lblBankIdent.Enabled = false;
                this.cbbBank.Enabled = false;
            }
            else                                  //银行卡      
            {
                this.lblBankIdent.Enabled = true;
                this.cbbBank.Enabled = true;
            }
        }

        #endregion

        #region PictureBox

        private void pbHide_Click(object sender, EventArgs e)
        {
            this.pLeft.Visible = false;
        }

        private void pbRefresh_Click(object sender, EventArgs e)
        {
            if (this.rbtnIncome.Checked) loadIncomeClassify();
            else loadExpenseClassify();
        }

        private void pbAdd_Click(object sender, EventArgs e)
        {
            Setting.Classify Classify = new Setting.Classify();
            Classify.ShowDialog();
            Classify.Dispose();

            if (this.rbtnExpense.Checked) loadExpenseClassify();
            else loadIncomeClassify();
        }

        #endregion

        #region DataGridView

        private void dgvList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}条记录", this.dgvList.RowCount);
        }

        #endregion

        #region ContextMenuStrip

        private void toolStripMenuItem_Delete_Click(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count == 0) return;

            List<string> guids = new List<string>();
            foreach (DataGridViewRow row in this.dgvList.SelectedRows) guids.Add(row.Cells[0].Value.ToString());

            if (MoneyClass.Expenses.Delete(guids) < 1)
            {
                MessageBox.Show("操作失败");
                return;
            }

            loadData();
        }

        #endregion

        #endregion

        #region Method

        private void InitIncomeType()
        {
            ArrayList Type = new ArrayList();
            Type.Add(new DictionaryEntry("0", "现金"));
            Type.Add(new DictionaryEntry("1", "银行卡"));
            this.cbbType.DisplayMember = "Value";
            this.cbbType.ValueMember = "Key";
            this.cbbType.DataSource = Type;
            this.cbbType.SelectedIndex = 1;
        }

        private void InitExpenseType()
        {
            ArrayList Type = new ArrayList();
            Type.Add(new DictionaryEntry("0", "现金消费"));
            Type.Add(new DictionaryEntry("1", "银行卡消费"));
            Type.Add(new DictionaryEntry("-1","取款"));
            this.cbbType.DisplayMember = "Value";
            this.cbbType.ValueMember = "Key";
            this.cbbType.DataSource = Type;
            this.cbbType.SelectedIndex = 0;
        }

        private void loadBank()
        {
            DataTable dt = MoneyClass.UserBank.Get(Main.CurrentGuid);
            this.cbbBank.DisplayMember = "Ident";
            this.cbbBank.ValueMember = "guid";
            this.cbbBank.DataSource = dt;
        }

        private void loadIncomeClassify()
        {
            DataTable dt = MoneyClass.Classify.getIncome(Main.CurrentGuid);
            this.cbbClassify.DisplayMember = "classify";
            this.cbbClassify.ValueMember = "guid";
            this.cbbClassify.DataSource = dt;
        }

        private void loadExpenseClassify()
        {
            DataTable dt = MoneyClass.Classify.getExpense(Main.CurrentGuid);
            this.cbbClassify.DisplayMember = "classify";
            this.cbbClassify.ValueMember = "guid";
            this.cbbClassify.DataSource = dt;
        }

        private void loadData()
        {
            DataTable dt = MoneyClass.Expenses.get(Main.CurrentGuid);
            this.dgvList.AutoGenerateColumns = false;
            this.dgvList.Columns[0].DataPropertyName = "guid";
            this.dgvList.Columns[1].DataPropertyName = "typeText";
            this.dgvList.Columns[2].DataPropertyName = "type";
            this.dgvList.Columns[3].DataPropertyName = "ident";
            this.dgvList.Columns[4].DataPropertyName = "classify";
            this.dgvList.Columns[5].DataPropertyName = "cid";
            this.dgvList.Columns[6].DataPropertyName = "thisDate";
            this.dgvList.Columns[7].DataPropertyName = "money";
            this.dgvList.Columns[8].DataPropertyName = "comment";
            this.dgvList.DataSource = dt;
            this.dgvList.AutoResizeColumns();
        }

        #endregion
    }
}
